package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.mall.MallPoster;
import com.tencreat.mapper.mall.MallPosterMapper;
import com.tencreat.service.mall.IMallPosterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 海报配置Service业务层处理
 *
 * @author tencreat
 * @date 2022-01-10
 */
@Service
public class MallPosterServiceImpl extends ServiceImpl<MallPosterMapper, MallPoster> implements IMallPosterService {


    @Autowired
    private MallPosterMapper mallPosterMapper;

    /**
     * 查询海报配置
     *
     * @param id 海报配置ID
     * @return 海报配置
     */
    @Override
    public MallPoster selectMallPosterById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询海报配置列表
     *
     * @param mallPoster 海报配置
     * @return 海报配置
     */
    @Override
    public List<MallPoster> selectMallPosterList(MallPoster mallPoster) {
        return baseMapper.selectList(Wrappers.query(mallPoster));
    }

    /**
     * 新增海报配置
     *
     * @param mallPoster 海报配置
     * @return 结果
     */
    @Override
    public int insertMallPoster(MallPoster mallPoster) {
        mallPoster.setCreateTime(LocalDateTime.now());
        return mallPosterMapper.insertMallPoster(mallPoster);
//         baseMapper.insert(mallPoster);
    }

    /**
     * 修改海报配置
     *
     * @param mallPoster 海报配置
     * @return 结果
     */
    @Override
    public int updateMallPoster(MallPoster mallPoster) {
        mallPoster.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallPoster);
    }

    /**
     * 批量删除海报配置
     *
     * @param ids 需要删除的海报配置ID
     * @return 结果
     */
    @Override
    public int deleteMallPosterByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除海报配置信息
     *
     * @param id 海报配置ID
     * @return 结果
     */
    @Override
    public int deleteMallPosterById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public int reSortOrderReduce(MallPoster mallPoster) {
        //查出上一条数据的ID
        Long sourceId = mallPosterMapper.getUpId(mallPoster.getSortOrder());
        //两个ID替换
        mallPosterMapper.exchange(mallPoster.getId(), sourceId);
        return 0;
    }

    @Override
    public int reSortOrderPlus(MallPoster mallPoster) {
        //查出下一条数据的ID
        Long sourceId = mallPosterMapper.getDownId(mallPoster.getSortOrder());
        //两个ID替换
        mallPosterMapper.exchange(mallPoster.getId(), sourceId);
        return 0;
    }
}
